//-----------------------------------------------------------------------
// <copyright file="TerritoryDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class TerritoryDAO : TerritoryDAOBase
	{
		public TerritoryDAO()
		{
		}

		public virtual TerritoryCollection GetTerritoriesByCountry(int countryID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesGetListByCountry");
                
				dbCommandWrapper.AddInParameter("@countryID", DbType.Int32, countryID);
                
				TerritoryCollection territoryCollection = new TerritoryCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						Territory territory = CreateTerritoryFromReader(reader);
						territoryCollection.Add(territory);
					}
					reader.Close();
				}
				
				return territoryCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetTerritoriesException, ex);
			}
		}

		public virtual TerritoryCollection GetTerritoriesForIDSByCountry(int countryID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesGetListForIDSByCountry");
                
				dbCommandWrapper.AddInParameter("@countryID", DbType.Int32, countryID);
                
				TerritoryCollection territoryCollection = new TerritoryCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						Territory territory = CreateTerritoryFromReader(reader);
						territoryCollection.Add(territory);
					}
					reader.Close();
				}
				
				return territoryCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetTerritoriesException, ex);
			}
		}
		#region override methods

	            
		public override void CreateTerritory(Territory territory)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesInsert");
                
				dbCommandWrapper.AddInParameter("@Description", DbType.String, territory.Description);
				dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, territory.CountryID==Nulls.NullInteger?DBNull.Value:(object)territory.CountryID);
				dbCommandWrapper.AddInParameter("@IDNumber", DbType.Int32, territory.IDNumber == Nulls.NullInteger?DBNull.Value:(object)territory.IDNumber);
				dbCommandWrapper.AddInParameter("@ShowHDPricingOnIDS", DbType.Boolean, territory.ShowHDPricingOnIDS);
				dbCommandWrapper.AddOutParameter("@TerritoryID", DbType.Int32, 0);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
				territory.TerritoryID = (int)dbCommandWrapper.GetParameterValue("@TerritoryID");
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateTerritoryException, ex);
			}
		}

	

		
        
		public override void UpdateTerritory(Territory territory)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spTerritoriesUpdate");            
                
				dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, territory.TerritoryID);
				dbCommandWrapper.AddInParameter("@Description", DbType.String, territory.Description);
				dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, territory.CountryID==Nulls.NullInteger?DBNull.Value:(object)territory.CountryID);
				dbCommandWrapper.AddInParameter("@IDNumber", DbType.Int32, territory.IDNumber == Nulls.NullInteger?DBNull.Value:(object)territory.IDNumber);
				dbCommandWrapper.AddInParameter("@ShowHDPricingOnIDS", DbType.Boolean, territory.ShowHDPricingOnIDS);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessUpdateTerritoryException, ex);
			}
		}
        


		#endregion
	}
}
